package com.sc.nft.service.impl;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sc.nft.dao.UserOrderAccountFlowDao;
import com.sc.nft.entity.LlUserExt;
import com.sc.nft.entity.UserAccountFlow;
import com.sc.nft.entity.UserInfo;
import com.sc.nft.entity.vo.PayBalanceDetailVO;
import com.sc.nft.entity.vo.PayBalanceVO;
import com.sc.nft.entity.vo.UserOrderAccountFlowVO;
import com.sc.nft.enums.PayChannelEnum;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.helper.LlPayHelper;
import com.sc.nft.helper.SandAccountHelper;
import com.sc.nft.mapper.ScUserAccountFlowMapper;
import com.sc.nft.pool.UserPool;
import com.sc.nft.service.LlUserExtService;
import com.sc.nft.service.ScUserAccountFlowService;
import com.sc.nft.service.UserInfoService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 用户三方钱包结算记录 服务实现类
 * </p>
 *
 * @author PC
 * @since 2024-10-22
 */
@Service
@RequiredArgsConstructor
public class ScUserAcountFlowServiceImpl extends ServiceImpl<ScUserAccountFlowMapper, UserAccountFlow> implements ScUserAccountFlowService {
    private final UserOrderAccountFlowDao userOrderAccountFlowDao;
    private final LlPayHelper llPayHelper;
    private final SandAccountHelper sandAccountHelper;
    private final UserInfoService userInfoService;
    private final LlUserExtService llUserExtService;

    @Override
    public UserOrderAccountFlowVO findByAPayChannel(PayChannelEnum channelEnum) {
        UserInfo user = UserPool.getUser();
        UserOrderAccountFlowVO flowVO = new UserOrderAccountFlowVO();
        BeanUtil.copyProperties(userOrderAccountFlowDao.queryOrInit(user.getId(), channelEnum), flowVO);
        return flowVO;
    }

    @Override
    public PayBalanceDetailVO getPayBalanceDetail(PayChannelEnum channelEnum, Date startTime, Date finishTime,
                                                  Integer changeType, Integer pageNo, Integer pageSize) {
        UserInfo user = UserPool.getUser();
        PayBalanceDetailVO vo = new PayBalanceDetailVO();
        List<PayBalanceVO> bvList = new ArrayList<>();
        switch (channelEnum) {
            case SAND:
                String sandResult = sandAccountHelper.queryAccountDetail(user,
                        DateUtil.format(startTime, "yyyyMMdd"),
                        DateUtil.format(finishTime, "yyyyMMdd"), pageNo.toString(), pageSize.toString(), changeType);
                JSONObject object = JSONObject.parseObject(sandResult);
                vo.setTotalSize(object.getIntValue("total"));
                vo.setPageNo(object.getIntValue("pageNo"));
                vo.setPageSize(object.getIntValue("pageSize"));
                JSONArray dataList = object.getJSONArray("dataList");
                if (!dataList.isEmpty()) {
                    for (int i = 0; i < dataList.size(); i++) {
                        JSONObject data = dataList.getJSONObject(i);
                        PayBalanceVO balanceVO = new PayBalanceVO();
                        balanceVO.setCurrentAmount(data.getString("actBalAft"));
                        balanceVO.setTransAmount(data.getString("actAmt"));
//                        balanceVO.setTransTime(data.getString("transTime"));
                        balanceVO.setTransTime(DateUtil.format(DateUtil.parse(data.getString("transTime"),
                                "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
                        balanceVO.setIoFlag(data.getString("ioFlag").equals("01") ? 1 : 0);
                        bvList.add(balanceVO);
                    }
                    vo.setList(bvList);
                }
                return vo;
            case LL_PAY:
                LlUserExt userExt = llUserExtService.getByUserId(user.getId());
                String llResult = llPayHelper.getBalanceDetail(userExt, startTime, finishTime, pageNo, pageSize, changeType);
                JSONObject jsonObject = JSONObject.parseObject(llResult);
                vo.setTotalSize(jsonObject.getIntValue("total_num"));
                vo.setPageNo(jsonObject.getIntValue("page_no"));
                vo.setTotalPages(jsonObject.getIntValue("total_page"));
                JSONArray acctbalList = jsonObject.getJSONArray("acctbal_list");
                if (!acctbalList.isEmpty()) {
                    for (int i = 0; i < acctbalList.size(); i++) {
                        JSONObject data = acctbalList.getJSONObject(i);
                        PayBalanceVO balanceVO = new PayBalanceVO();
                        balanceVO.setCurrentAmount(data.getString("amt_bal"));
                        balanceVO.setTransAmount(data.getString("amt"));
                        balanceVO.setTransTime(DateUtil.format(DateUtil.parse(data.getString("txn_time"),
                                "yyyyMMddHHmmss"), "yyyy-MM-dd HH:mm:ss"));
                        balanceVO.setIoFlag(data.getString("flag_dc").equals("DEBIT") ? 1 : 0);
                        bvList.add(balanceVO);
                    }
                    vo.setList(bvList);
                }

                return vo;
            default:
                throw new GlobalRunTimeException("暂不支持");
        }
    }
}
